﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OracleDataToRedis.Utils;
using OracleDataToRedis.Domain;

namespace OracleDataToRedis.Coders.WcfModels
{
    public class BaseMainServiceCoder
    {
        public static void Write()
        {
            string path = BaseParams.WcfModelsPath;
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, "IMainService.cs");

            #region 创建IMainService文件
            FileStream file = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(file, Encoding.UTF8);

            CommentsCoder.CreateCsComments("IMainService", sw);

            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Runtime.Serialization;");
            sw.WriteLine("using System.ServiceModel;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using System.Data;");
            sw.WriteLine("using " + BaseParams.UtilityNameSpace + ";");
            sw.WriteLine("");
            sw.WriteLine("namespace " + BaseParams.WcfModelsNameSpace + "");
            sw.WriteLine("{");
            sw.WriteLine("    // 注意: 如果更改此处的接口名称 \"IMainService\"，也必须更新 App.config 中对 \"IMainService\" 的引用。");
            sw.WriteLine("    [ServiceContract]");
            sw.WriteLine("    public interface IMainService");
            sw.WriteLine("    {");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void DoWork();");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存或更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectSaveOrUpdate(ref MyWcfTranserObject transObj);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存或更新对象列表");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectSaveOrUpdateList(ref MyWcfTranserObject transObj);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectSave(ref MyWcfTranserObject transObj);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        /// <param name=\"key\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectUpdate(ref MyWcfTranserObject transObj, object key);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectDelete(MyWcfTranserObject transObj);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象列表");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        void ObjectDeleteList(MyWcfTranserObject transObj);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 获取对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"MyWcfTranserObject\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        MyWcfTranserObject ObjectGet(MyWcfTranserObject MyWcfTranserObject);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 获取对应对象所有对象集合");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"qt\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        MyWcfTranserObject ObjectAll(string objectTypeName, QueryType qt);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL条件货物对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"condi\"></param>");
            sw.WriteLine("        /// <param name=\"qt\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        MyWcfTranserObject GetObjectByHqlCondi(string objectTypeName, string condi, QueryType qt, int maxDepth, ref Paging paging, params Object[] ps);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL查询语句获取对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        MyWcfTranserObject GetObjectByHqlQuery(string objectTypeName, String hql, int maxDepth, ref Paging paging, params Object[] ps);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过执行Sql语句，返回DataTable");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        DataTable SqlFillDataTable(String sql, params Object[] ps);");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 调用服务端事物方法的简单通用方法");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"wcfTransObjs\"></param>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"transMethodName\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        [OperationContract]");
            sw.WriteLine("        MyWcfTranserObject ExcuteTransationMethod(MyWcfTranserObject wcfTransObjs, string objectTypeName, string transMethodName);");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion

            path = BaseParams.WcfModelsPath;
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, "MainService.cs");

            #region 创建MainService文件
            file = new FileStream(path, FileMode.Create);
            sw = new StreamWriter(file, Encoding.UTF8);

            CommentsCoder.CreateCsComments("MainService", sw);

            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Runtime.Serialization;");
            sw.WriteLine("using System.ServiceModel;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using System.Data;");
            sw.WriteLine("using System.Reflection;");
            sw.WriteLine("using log4net;");
            sw.WriteLine("using " + BaseParams.UtilityNameSpace + ";");
            sw.WriteLine("");
            sw.WriteLine("namespace " + BaseParams.WcfModelsNameSpace + "");
            sw.WriteLine("{");
            sw.WriteLine("    // 注意: 如果更改此处的类名 \"MainService\"，也必须更新 App.config 中对 \"MainService\" 的引用。");
            sw.WriteLine("    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]");
            sw.WriteLine("    public partial class MainService : IMainService");
            sw.WriteLine("    {");
            sw.WriteLine("        private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
            sw.WriteLine("");
            sw.WriteLine("        public void DoWork()");
            sw.WriteLine("        {");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存或更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        public void ObjectSaveOrUpdate(ref MyWcfTranserObject transObj)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteSaveOrUpdate(transObj);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存或更新对象列表");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        public void ObjectSaveOrUpdateList(ref MyWcfTranserObject transObj)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteSaveOrUpdateList(transObj);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        public void ObjectSave(ref MyWcfTranserObject transObj)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteSave(transObj);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        /// <param name=\"key\"></param>");
            sw.WriteLine("        public void ObjectUpdate(ref MyWcfTranserObject transObj, object key)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteUpdate(transObj, key);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        public void ObjectDelete(MyWcfTranserObject transObj)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteDelete(transObj);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象列表");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"transObj\"></param>");
            sw.WriteLine("        public void ObjectDeleteList(MyWcfTranserObject transObj)");
            sw.WriteLine("        {");
            sw.WriteLine("            MainServiceFactory.Instance.ExcuteDeleteList(transObj);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 获取对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"MyWcfTranserObject\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public MyWcfTranserObject ObjectGet(MyWcfTranserObject MyWcfTranserObject)");
            sw.WriteLine("        {");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteObjectGet(MyWcfTranserObject);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 获取对应对象所有对象集合");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"qt\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public MyWcfTranserObject ObjectAll(string objectTypeName, QueryType qt)");
            sw.WriteLine("        {");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteObjectAll(objectTypeName, qt);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL条件货物对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"condi\"></param>");
            sw.WriteLine("        /// <param name=\"qt\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public MyWcfTranserObject GetObjectByHqlCondi(string objectTypeName, string condi, QueryType qt, int maxDepth, ref Paging paging, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteGetObjectByHqlCondi(objectTypeName, condi, qt, maxDepth, paging, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL查询语句获取对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"paging\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public MyWcfTranserObject GetObjectByHqlQuery(string objectTypeName, String hql, int maxDepth, ref Paging paging, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteGetObjectByHqlQuery(objectTypeName, hql, maxDepth, paging, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过执行Sql语句，返回DataTable");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public DataTable SqlFillDataTable(String sql, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            object o = new object();");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteSqlFillDataTable(sql, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 调用服务端事物方法的简单通用方法");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"wcfTransObjs\"></param>");
            sw.WriteLine("        /// <param name=\"objectTypeName\"></param>");
            sw.WriteLine("        /// <param name=\"transMethodName\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public MyWcfTranserObject ExcuteTransationMethod(MyWcfTranserObject wcfTransObjs, string objectTypeName, string transMethodName)");
            sw.WriteLine("        {");
            sw.WriteLine("            return MainServiceFactory.Instance.ExcuteTransationMethod(wcfTransObjs, objectTypeName, transMethodName);");
            sw.WriteLine("        }");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion
        }
    }
}
